package com.jinke.api.modules.ums.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinke.api.common.api.CommonPage;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.modules.app.model.UserRecharge;
import com.jinke.api.modules.app.request.UserRechargeRequest;
import com.jinke.api.modules.ums.model.UmsAdminLog;
import com.jinke.api.modules.ums.request.AdminLogPageRequest;
import com.jinke.api.modules.ums.request.LogQueryRequest;
import com.jinke.api.modules.ums.service.UmsAdminLogService;
import com.jinke.api.modules.ums.vo.AdminLogVO;
import com.jinke.api.security.util.AdminUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author macro
 * @since 2023-07-13
 */
@RestController
@RequestMapping("/admin/log")
@Api(tags = "用户请求日志")
@Tag(name = "用户请求日志", description = "用户请求日志")
@RequiredArgsConstructor
public class AdminLogController {

    private final UmsAdminLogService umsAdminLogService;

    @ApiOperation("请求日志列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<UmsAdminLog>> list(LogQueryRequest request) {
        Page<UmsAdminLog> list = umsAdminLogService.list(request);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("用户日志分页")
    @GetMapping
    @ResponseBody
    public CommonResult<CommonPage<AdminLogVO>> list(AdminLogPageRequest request) {
        Page<UmsAdminLog> umsAdminLogPage = Optional.ofNullable(
                umsAdminLogService.page(
                        new Page<>(request.getPageNum(), request.getPageSize()),
                        Wrappers.<UmsAdminLog>lambdaQuery()
                                .like(StrUtil.isNotBlank(request.getUsername()), UmsAdminLog::getUsername, request.getUsername())
                                .select(UmsAdminLog::getAdminId, UmsAdminLog::getUsername, UmsAdminLog::getUri, UmsAdminLog::getParams,
                                        UmsAdminLog::getMethod, UmsAdminLog::getIp, UmsAdminLog::getStatusCode, UmsAdminLog::getMessage,
                                        UmsAdminLog::getCreatedAt).orderByDesc(UmsAdminLog::getCreatedAt))
        ).orElse(new Page<UmsAdminLog>());
        return CommonResult.success(CommonPage.restPage(
                new Page<AdminLogVO>()
                        .setCurrent(umsAdminLogPage.getCurrent())
                        .setSize(umsAdminLogPage.getSize())
                        .setTotal(umsAdminLogPage.getTotal())
                        .setRecords(
                                Optional.ofNullable(umsAdminLogPage.getRecords()).orElse(Lists.newArrayList()).stream().map(umsAdminLog -> {
                                    AdminLogVO adminLogVO = new AdminLogVO();
                                    BeanUtils.copyProperties(umsAdminLog, adminLogVO);
                                    return adminLogVO;
                                }).collect(Collectors.toList())
                        )));
    }
}

